package cn.ecut.dzy.interrupt;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: juc
 * @description:
 * @author: DingZhenYun
 * @create: 2021-10-25 21:00
 **/

public class LockSupportDemo {
    static Object objectLock = new Object();
    static Lock lock = new ReentrantLock();
    private static Condition condition = lock.newCondition();

    public static void main(String[] args) {

        /**
         * LockSupport：俗称 锁中断
         * 以前的两种方式：
         * 		1.以前的等待唤醒通知机制必须synchronized里面有一个wait和notify
         * 		2.lock里面有await和signal
         * 这上面这两个都必须要持有锁才能干
         * LockSupport它的解决的痛点
         * 		1.LockSupport不用持有锁块，不用加锁，程序性能好，
         * 		2.先后顺序，不容易导致卡死
         */

         Thread t1=new Thread(() ->{
             System.out.println(Thread.currentThread().getName() + "\t" + "---come in");
             LockSupport.park();
             System.out.println(Thread.currentThread().getName() + "\t" + "---被唤醒");
        },"T1");
        t1.start();

        Thread t2= new Thread(() ->{
            System.out.println(Thread.currentThread().getName() + "\t" + "---come in");
            LockSupport.unpark(t1);
            System.out.println(Thread.currentThread().getName() + "\t" + "---被唤醒");
        },"T2");
        t2.start();
    }

    public static void lockAwaitSignal()
    {
        new Thread(() -> {
            //暂停几秒钟线程
//            try {
//                TimeUnit.SECONDS.sleep(3);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            lock.lock();
            try
            {
                System.out.println(Thread.currentThread().getName()+"\t"+"---come in");
                condition.await();
                System.out.println(Thread.currentThread().getName()+"\t"+"---被唤醒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"t1").start();

        new Thread(() -> {
            lock.lock();
            try
            {
                condition.signal();
                System.out.println(Thread.currentThread().getName()+"\t"+"---发出通知");
            }finally {
                lock.unlock();
            }
        },"t2").start();
    }

    public static void syncWaitNotify() {
        new Thread(() -> {
            //暂停几秒钟线程
//            try {
//                TimeUnit.SECONDS.sleep(3);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            synchronized (objectLock) {
                System.out.println(Thread.currentThread().getName() + "\t" + "---come in");
                try {
                    objectLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "\t" + "---被唤醒");
            }
        }, "t1").start();

        //暂停几秒钟线程
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            synchronized (objectLock) {
                objectLock.notify();
                System.out.println(Thread.currentThread().getName() + "\t" + "---发出通知");
            }
        }, "t2").start();
    }
}
